<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Grid\AuthenticationAction;
use App\Admin\Repositories\WxAuthentication;
use App\Models\Shop\WxShopService;
use App\Models\User\WxUser;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class WxAuthenticationController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new WxAuthentication(['user']), function (Grid $grid) {
            $grid->simplePaginate();
            $grid->quickSearch(['user_id', 'user.user_name', 'name', 'introduce','contact_information', 'overrule_content'])->placeholder('搜索用户id，用户名，认证名称，介绍，联系方式，驳回原因...')->width(35);
            $grid->model()->orderBy('id','desc');
            $grid->column('user_id','用户ID');
            $grid->column('user.user_name','用户');
            $grid->column('name')->editable();
            $grid->column('introduce')->limit(12);
            $grid->column('contact_information');
            $grid->column('identity_picture')->image('',60);
            $grid->column('overrule_content')->limit(12);
//            $grid->column('overrule_is_read');
            $grid->column('authentication_state')->using([
                0=>'审核中',
                1=>'审核通过',
                2=>'驳回'
            ])->label([
                0=>'red',
                1=>'green',
                2=>'default'
            ])->sortable();
            $grid->column('created_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });

            // 也可以通过这种方式添加
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->append(new AuthenticationAction());
            });

            $grid->actions(function (Grid\Displayers\Actions $actions) {
//                $actions->disableDelete();
//                $actions->disableEdit();
                $actions->disableQuickEdit();
                $actions->disableView();
            });
//            $grid->disableCreateButton();

        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new WxAuthentication(), function (Show $show) {
            $show->field('id');
            $show->field('user_id');
            $show->field('name');
            $show->field('introduce');
            $show->field('contact_information');
            $show->field('identity_picture');
            $show->field('overrule_content');
            $show->field('overrule_is_read');
            $show->field('authentication_state');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new WxAuthentication(), function (Form $form) {
            $form->display('id');
//            $form->select('user_id','用户')
//                ->options(WxUser::limit(10)->pluck('user_name','id'));
            $form->text('user_id');
            $form->text('name');
            $form->text('introduce');
            $form->text('contact_information');
            $form->image('identity_picture')->url('files/uploads')->uniqueName()->autoUpload();
            $form->text('overrule_content');
//            $form->text('overrule_is_read');
            $form->radio('authentication_state')->options([0=>'审核中',
                1=>'审核通过',
                2=>'驳回']);

            $form->deleting(function (Form $form){
                global $__MINI_GLOBAL_TENANT_ID__;
                if($__MINI_GLOBAL_TENANT_ID__ > 0){
                    return $form->response()->error('权限不足，不可以删除其他分站对象');
                }
            });

        });
    }
}
